
#include "stdafx.h"
#include "MainFrm.h"
#include "ClassView.h"
#include "Resource.h"
#include "XortStudio.h"

class CClassViewMenuButton : public CMFCToolBarMenuButton
{
	friend class CClassView;

	DECLARE_SERIAL(CClassViewMenuButton)

public:
	CClassViewMenuButton(HMENU hMenu = NULL) : CMFCToolBarMenuButton((UINT)-1, hMenu, -1)
	{
	}

	virtual void OnDraw(CDC* pDC, const CRect& rect, CMFCToolBarImages* pImages, BOOL bHorz = TRUE,
		BOOL bCustomizeMode = FALSE, BOOL bHighlight = FALSE, BOOL bDrawBorder = TRUE, BOOL bGrayDisabledButtons = TRUE)
	{
		pImages = CMFCToolBar::GetImages();

		CAfxDrawState ds;
		pImages->PrepareDrawImage(ds);

		CMFCToolBarMenuButton::OnDraw(pDC, rect, pImages, bHorz, bCustomizeMode, bHighlight, bDrawBorder, bGrayDisabledButtons);

		pImages->EndDrawImage(ds);
	}
};

IMPLEMENT_SERIAL(CClassViewMenuButton, CMFCToolBarMenuButton, 1)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CClassView::CClassView()
{
	m_nCurrSort = ID_SORTING_GROUPBYTYPE;
}

CClassView::~CClassView()
{
}

BEGIN_MESSAGE_MAP(CClassView, CDockablePane)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_CONTEXTMENU()
	ON_COMMAND(ID_CLASS_ADD_MEMBER_FUNCTION, OnClassAddMemberFunction)
	ON_COMMAND(ID_CLASS_ADD_MEMBER_VARIABLE, OnClassAddMemberVariable)
	ON_COMMAND(ID_CLASS_DEFINITION, OnClassDefinition)
	ON_COMMAND(ID_CLASS_PROPERTIES, OnClassProperties)
	ON_COMMAND(ID_NEW_FOLDER, OnNewFolder)
	ON_WM_PAINT()
	ON_WM_SETFOCUS()
	ON_COMMAND_RANGE(ID_SORTING_GROUPBYTYPE, ID_SORTING_SORTBYACCESS, OnSort)
	ON_UPDATE_COMMAND_UI_RANGE(ID_SORTING_GROUPBYTYPE, ID_SORTING_SORTBYACCESS, OnUpdateSort)
    ON_NOTIFY(NM_DBLCLK, 2, OnClassTreeNotify)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClassView message handlers

int CClassView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDockablePane::OnCreate(lpCreateStruct) == -1)
		return -1;

	CRect rectDummy;
	rectDummy.SetRectEmpty();

	// Create views:
	const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;

	if (!m_wndClassView.Create(dwViewStyle, rectDummy, this, 2))
	{
		TRACE0("Failed to create Class View\n");
		return -1;      // fail to create
	}
    //m_wndClassView.SetBkColor( RGB(41, 163, 143) );

	// Load images:
	m_wndToolBar.Create(this, AFX_DEFAULT_TOOLBAR_STYLE, IDR_SORT);
	m_wndToolBar.LoadToolBar(IDR_SORT, 0, 0, TRUE /* Is locked */);

	OnChangeVisualStyle();

	m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() | CBRS_TOOLTIPS | CBRS_FLYBY);
	m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));

	m_wndToolBar.SetOwner(this);

	// All commands will be routed via this control , not via the parent frame:
	m_wndToolBar.SetRouteCommandsViaFrame(FALSE);

	CMenu menuSort;
	menuSort.LoadMenu(IDR_POPUP_SORT);

	m_wndToolBar.ReplaceButton(ID_SORT_MENU, CClassViewMenuButton(menuSort.GetSubMenu(0)->GetSafeHmenu()));

	CClassViewMenuButton* pButton =  DYNAMIC_DOWNCAST(CClassViewMenuButton, m_wndToolBar.GetButton(0));

	if (pButton != NULL)
	{
		pButton->m_bText = FALSE;
		pButton->m_bImage = TRUE;
		pButton->SetImage(GetCmdMgr()->GetCmdImage(m_nCurrSort));
		pButton->SetMessageWnd(this);
	}
    	
	return 0;
}

void CClassView::OnSize(UINT nType, int cx, int cy)
{
	CDockablePane::OnSize(nType, cx, cy);
	AdjustLayout();
}

void CClassView::ShowXRTContent( LPCSTR lpszFileName, XRuntime *pRT )
{
    LPCSTR p = strrchr( lpszFileName, '\\' );
    p++;
    HTREEITEM hRoot = m_wndClassView.InsertItem( p, icn_Module, icn_Module );
    m_wndClassView.SetItemState( hRoot, TVIS_BOLD, TVIS_BOLD );

    HTREEITEM hConstants = m_wndClassView.InsertItem( "Constants", icn_Folder, icn_Folder, hRoot );
    HTREEITEM hMethods   = m_wndClassView.InsertItem( "Methods", icn_Folder, icn_Folder, hRoot );
    HTREEITEM hVars      = m_wndClassView.InsertItem( "Variables", icn_Folder, icn_Folder, hRoot );

    HTREEITEM hItem;
    SYMBOLLIST listGlobal = pRT->get_GlobalSymbolList();
    SYMBOL_LIST::iterator theIter = listGlobal.end();
    symbolref pSym = (symbolref)theIter;
    while( pSym )
    {
        hItem = NULL;

        if( pSym->m_SymKind == sk_var )
        {
            if( pSym->m_dwFlags & f_const ) {
                hItem = m_wndClassView.InsertItem( TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE, pSym->m_csName.GetBuffer(), icn_PublicVariable, icn_PublicVariable, 0, 0, 0, hConstants, TVI_SORT );
            } else {
                hItem = m_wndClassView.InsertItem( TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE, pSym->m_csName.GetBuffer(), icn_PublicVariable, icn_PublicVariable, 0, 0, 0, hVars, TVI_SORT );
            }
        }
        else
        if( pSym->m_SymKind == sk_obj )
        {
            hItem = m_wndClassView.InsertItem( pSym->m_csName.GetBuffer(), icn_Class, icn_Class, hRoot );
            ShowClass( hItem, pSym );
        }
        else
        if( pSym->m_SymKind == sk_proc )
        {
            hItem = m_wndClassView.InsertItem( TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE, pSym->m_csName.GetBuffer(), icn_PublicMethod, icn_PublicMethod, 0, 0, 0, hMethods, TVI_SORT );
        }

        if( hItem )
            m_wndClassView.SetItemData( hItem, (DWORD_PTR)pSym );

        theIter--;
        pSym = theIter != NULL ? (symbolref)theIter : NULL;
    }
    
    m_wndClassView.Expand( hRoot, TVE_EXPAND );
}

void CClassView::ShowClass( HTREEITEM hClass, symbolref pCls )
{
    SYMBOL_LIST::iterator theIter = pCls->m_listMembers.end();
    symbolref pSym = (symbolref)theIter;
    while( pSym )
    {
        HTREEITEM hItem = NULL;

        if( pSym->m_SymKind == sk_var )
        {
            hItem = m_wndClassView.InsertItem( TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE, pSym->m_csName.GetBuffer(), icn_PublicVariable, icn_PublicVariable, 0, 0, (LPARAM)pSym, hClass, TVI_LAST );
        }
        else
        if( pSym->m_SymKind == sk_obj )
        {
            hItem = m_wndClassView.InsertItem( pSym->m_csName.GetBuffer(), icn_Class, icn_Class, hClass );
            ShowClass( hItem, pSym );
        }
        else
        if( pSym->m_SymKind == sk_proc )
        {
            hItem = m_wndClassView.InsertItem( TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE, pSym->m_csName.GetBuffer(), icn_PublicMethod, icn_PublicMethod, 0, 0, (LPARAM)pSym, hClass, TVI_LAST );
        }

        if( hItem )
            m_wndClassView.SetItemData( hItem, (DWORD_PTR)pSym );

        theIter--;
        pSym = theIter != NULL ? (symbolref)theIter : NULL;
    }
}

void CClassView::OnContextMenu(CWnd* pWnd, CPoint point)
{
	CTreeCtrl* pWndTree = (CTreeCtrl*)&m_wndClassView;
	ASSERT_VALID(pWndTree);

	if (pWnd != pWndTree)
	{
		CDockablePane::OnContextMenu(pWnd, point);
		return;
	}

	if (point != CPoint(-1, -1))
	{
		// Select clicked item:
		CPoint ptTree = point;
		pWndTree->ScreenToClient(&ptTree);

		UINT flags = 0;
		HTREEITEM hTreeItem = pWndTree->HitTest(ptTree, &flags);
		if (hTreeItem != NULL)
		{
			pWndTree->SelectItem(hTreeItem);
		}
	}

	pWndTree->SetFocus();
	CMenu menu;
	menu.LoadMenu(IDR_POPUP_SORT);

	CMenu* pSumMenu = menu.GetSubMenu(0);

	if (AfxGetMainWnd()->IsKindOf(RUNTIME_CLASS(CMDIFrameWndEx)))
	{
		CMFCPopupMenu* pPopupMenu = new CMFCPopupMenu;

		if (!pPopupMenu->Create(this, point.x, point.y, (HMENU)pSumMenu->m_hMenu, FALSE, TRUE))
			return;

		((CMDIFrameWndEx*)AfxGetMainWnd())->OnShowPopupMenu(pPopupMenu);
		UpdateDialogControls(this, FALSE);
	}
}

void CClassView::AdjustLayout()
{
	if (GetSafeHwnd() == NULL)
	{
		return;
	}

	CRect rectClient;
	GetClientRect(rectClient);

	int cyTlb = m_wndToolBar.CalcFixedLayout(FALSE, TRUE).cy;

	m_wndToolBar.SetWindowPos(NULL, rectClient.left, rectClient.top, rectClient.Width(), cyTlb, SWP_NOACTIVATE | SWP_NOZORDER);
	m_wndClassView.SetWindowPos(NULL, rectClient.left + 1, rectClient.top + cyTlb + 1, rectClient.Width() - 2, rectClient.Height() - cyTlb - 2, SWP_NOACTIVATE | SWP_NOZORDER);
}

BOOL CClassView::PreTranslateMessage(MSG* pMsg)
{
	return CDockablePane::PreTranslateMessage( pMsg );
}

void CClassView::OnSort(UINT id)
{
	if( m_nCurrSort == id )
	{
		return;
	}

	m_nCurrSort = id;

	CClassViewMenuButton* pButton =  DYNAMIC_DOWNCAST(CClassViewMenuButton, m_wndToolBar.GetButton(0));

	if( pButton != NULL )
	{
		pButton->SetImage(GetCmdMgr()->GetCmdImage(id));
		m_wndToolBar.Invalidate();
		m_wndToolBar.UpdateWindow();
	}
}

void CClassView::OnUpdateSort(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck(pCmdUI->m_nID == m_nCurrSort);
}

void CClassView::OnClassAddMemberFunction()
{
	AfxMessageBox(_T("Add member function..."));
}

void CClassView::OnClassAddMemberVariable()
{
	// TODO: Add your command handler code here
}

void CClassView::OnClassDefinition()
{
	// TODO: Add your command handler code here
}

void CClassView::OnClassProperties()
{
	// TODO: Add your command handler code here
}

void CClassView::OnNewFolder()
{
	AfxMessageBox(_T("New Folder..."));
}

void CClassView::OnPaint()
{
	CPaintDC dc(this); // device context for painting

	CRect rectTree;
	m_wndClassView.GetWindowRect(rectTree);
	ScreenToClient(rectTree);

	rectTree.InflateRect(1, 1);
	dc.Draw3dRect(rectTree, ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DSHADOW));
}

void CClassView::OnSetFocus(CWnd* pOldWnd)
{
	CDockablePane::OnSetFocus(pOldWnd);

	m_wndClassView.SetFocus();
}

void CClassView::OnChangeVisualStyle()
{
	m_ClassViewImages.DeleteImageList();

	UINT uiBmpId = theApp.m_bHiColorIcons ? IDB_CLASS_VIEW_24 : IDB_CLASS_VIEW;

	CBitmap bmp;
	if (!bmp.LoadBitmap(uiBmpId))
	{
		TRACE(_T("Can't load bitmap: %x\n"), uiBmpId);
		ASSERT(FALSE);
		return;
	}

	BITMAP bmpObj;
	bmp.GetBitmap(&bmpObj);

	UINT nFlags = ILC_MASK;

	nFlags |= (theApp.m_bHiColorIcons) ? ILC_COLOR24 : ILC_COLOR4;

	m_ClassViewImages.Create(16, bmpObj.bmHeight, nFlags, 0, 0);
	m_ClassViewImages.Add(&bmp, RGB(255, 0, 0));

	m_wndClassView.SetImageList(&m_ClassViewImages, TVSIL_NORMAL);

	m_wndToolBar.CleanUpLockedImages();
	m_wndToolBar.LoadBitmap(theApp.m_bHiColorIcons ? IDB_SORT_24 : IDR_SORT, 0, 0, TRUE /* Locked */);
}

void CClassView::OnClassTreeNotify(NMHDR* pNMHDR, LRESULT* pResult)
{
    if( pNMHDR )
    {
        if( pNMHDR->code == NM_DBLCLK )
        {
            DWORD dwPos = GetMessagePos();
            CPoint pt( GET_X_LPARAM( dwPos ), GET_Y_LPARAM ( dwPos ) ); 

            m_wndClassView.ScreenToClient( &pt ); 

            UINT test; 
            HTREEITEM hti = m_wndClassView.HitTest( pt, &test ); 
            
            if( hti != NULL && ( test & TVHT_ONITEM ) ) // Is the click actually *on* the item?
            { 
                HTREEITEM htCur = m_wndClassView.GetSelectedItem();
                CString csText = m_wndClassView.GetItemText( htCur );
                TRACE( "CViewTree::OnClassTreeNotify() NM_DBLCLK -> %s\n", csText );

                symbolref pSym = (symbolref)m_wndClassView.GetItemData( htCur );
                if( pSym )
                {
                    CXortStudioDoc *pDoc = GET_ACTIVE_DOC(CXortStudioDoc);
                    pDoc->LocateSymbolInView( pSym );
                }
            }
        }
    }

    *pResult = TRUE;
}
